package com.code.soulnetty.modbus.client.message;

import com.code.soulnetty.modbus.biz.msg.Message;
import com.google.common.collect.ImmutableSortedSet;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
 * MODBUS TCP请求
 *
 **/
@Slf4j
@ToString(callSuper = true)
public class ModbusTcpReadRequest extends ModbusReadRequest {

    public ModbusTcpReadRequest(ImmutableSortedSet<Message> requestList) {
        super(requestList, 12, true);
    }

    public ModbusTcpReadRequest(ImmutableSortedSet<Message> requestList, short msgId) {
        super(requestList, 12, msgId);
    }

    @Override
    int handleAduHeader() {
        if (!autoMsg) {
            setShort(data, 0, msgId);
        }
        //modbus 协议
        setShort(data, 2, (short) 0);
        //数据长度
        setShort(data, 4, (short) 6);
        //单元标示符
        data[6] = (byte) requestList.first().getUnitId();
        return 7;
    }

    @Override
    void handleAduTail(int index) {
        // this no needed
    }

}
